package com.bdqn.controller;


import com.bdqn.component.BlogConstants;
import com.bdqn.entity.BannerWithBLOBs;
import com.bdqn.service.ArticleTypeService;
import com.bdqn.service.BannerService;
import com.bdqn.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CurrentController {

    @Autowired
    private BannerService bannerService;
    @Autowired
    private ArticleTypeService articleTypeService;

    @GetMapping("/client/{view}.html")
    public String view(@PathVariable("view")String view,
                       Model model,
                       HttpSession session){

        session.setAttribute("token", TokenUtil.getToken(session));
        if(view.equals("home")){
            //加载主页数据
            Result<List<BannerWithBLOBs>> rs = bannerService.getBanner(null, StatusType.AVAILABLE);
            if (rs.isSuccess()){
                model.addAttribute("banners",rs.getData());
            }else {
                model.addAttribute("msg",rs.getContent());
            }
            //推荐博客的类型
            model.addAttribute("articleTypes",articleTypeService.getAllArticleType());
        }else if (view.equals("user-center") || view.equals("user-blog")){
            model.addAttribute("articleTypes",articleTypeService.getAllArticleType());
        }
        return "client/"+view;
    }


    /**
     * 获得加密字符串
     * @param token
     * @param session
     * @return
     */
    @GetMapping("/getEncode")
    @ResponseBody
    public Map<String,Object> getEncode(String token, HttpSession session){
        Map<String,Object> map = new HashMap<String, Object>();
        String scsstionTonken = (String) session.getAttribute("token");
        if (StringUtils.isBlank(token) || StringUtils.isBlank(scsstionTonken) ||!scsstionTonken.equals(token))
        {
            map.put("error","非法请求");
        }else {

            map.put("code",TokenUtil.encodeStr);
            
        }
        return map;
    }


    /**
     * 验证码
     * @param request
     * @param response
     */
    @GetMapping("/kaptcha")
    public void kaptcha(HttpServletRequest request,HttpServletResponse response ){
        KaptchaUtil.responseImg( request,response);
    }


    @GetMapping("/loginIsLive")
    @ResponseBody
    public  Result loginIsLive(HttpSession session){
        Object obj = session.getAttribute(BlogConstants.LOGIN_SESSION_KEY);
        if (obj == null ){
          return   Result.createError("登录已经失效");
        }

        return Result.createSuccess("登录未失效");
    }


}
